package common.queue;

public class LinkedDeque<E> {

    // Attribute
    private Node<E> head;
    private Node<E> tail;
    private int size ;
    private int count ;

    public LinkedDeque() {
        // Hier muss etwas angepasst werden
        this.size = 16;
        this.count = 0;
    }



    class Node<E> {
        private E value;
        private Node<E> next;
        private Node<E> prev;

        public Node(E value, Node<E> next, Node<E> prev) {
            this.value = value;
            this.prev = prev;
            this.next = next;
        }

        public E getValue() {
            return value;
        }

        public void setValue(E value) {
            this.value = value;
        }

        public Node<E> getNext() {
            return next;
        }

        public void setNext(Node<E> next) {
            this.next = next;
        }

        public Node<E> getPrev() {
            return prev;
        }

        public void setPrev(Node<E> prev) {
            this.prev = prev;
        }
    }
    
    /**
     * Inserts the specified element at the end of this deque.
     *
     * @param element the element to add
     * @return true, if the insertion was successful
     */
    public boolean offer(E element) {
        if (count == size) {
            return false;
        }
        Node<E> newNode = new Node<>(element, null, null);
        if (head == null) {
            head = new Node<E>(element, null, null);
            tail = head;
        } else {
            tail.setNext(newNode);
            newNode.setPrev(tail);
            tail = newNode;
        }
        count++;
        return true;
    }

    /**
     * Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
     *
     * @return the head of this deque, or null if this deque is empty
     */
    public E peekFirst() {
        if (count == 0) {
            return null;
        }
        return head.getValue();
    }

    /**
     * Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
     *
     * @return the tail of this deque, or null if this deque is empty
     */
    public E peekLast() {
        if (count == 0) {
            return null;
        }
        return tail.getValue();
    }

    /**
     * Retrieves and removes the head of the common.queue represented by this deque (in other words,
     * the first element of this deque), or returns null if this deque is empty.
     *
     * @return the head of the common.queue represented by this deque, or null if this deque is empty
     */
    public E poll() {
        if (count == 0) {
            return null;
        }
        E e = head.value;
        if (count == 1) {
            head = null;
            tail = null;
        } else {
            head = head.next;
            head.prev = null;
        }
        count--;
        return e;
    }
    
    /**
     * Retrieves and removes the last element of this deque, or returns null if this deque is empty.
     * 
     * @return the tail of this deque, or null if this deque is empty
     */
    public E pollLast() {
        if (count == 0) {
            return null;
        }
        E e = tail.value;
        if (count == 1) {
            head = null;
            tail = null;
        } else {
            tail = tail.prev;
            tail.next = null;
        }
        count--;
        return e;
    }

    /**
     * Pushes an element onto the common.stack represented by this deque.
     * In other words, inserts the element at the front of this deque.
     *
     * @param element the element to push
     * @return true, if the insertion was successful
     */
    public boolean push(E element) {
        if (count == size) {
            return false;
        }
        if (count == 0) {
            head = new Node<E>(element, null, null);
            tail = head;
        } else {
            Node<E> node = new Node<>(element, null, null);
            node.next = head;
            head = node;
        }
        count++;
        return true;
    }

    /**
     * Pops an element from the common.stack represented by this deque.
     * In other words, removes and returns the first element of this deque.
     *
     * @return the element at the front of this deque (which is the top of the common.stack represented by this deque)
     */
    public E pop() {
        return poll();
    }

    /**
     * Returns the number of elements in this deque.
     *
     * @return the number of elements in this deque
     */
    public int size() {
        return count;
    }

    /**
     * Returns the current capacity of the common.list.
     * 
     * @return the current capacity of the array in this common.list
     */
    
    int capacity() {
        return size;
    }
    
}
